/**********+++++++++++*******************************************************************
 * Copyright (c) 2022, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file main.c
 * @author Nations
 * @version V1.2.2
 *
 * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
 */

#include "sys_param.h"
/*******************************************
 * VCC = 3.0V, 即 3.0v----4096
 * 2024 11 26 采用2级分压方式dac输出范围必须是2.0v~2.9v
 *              dac处的电压越低，ems输出端电压越高
 * 
 * 去年第一次试产版本电压(500欧姆电阻)：
 * 模式1
 * 1档VPP=30V， 2档34v     3档，36v  4档38v     5档，40v 
 * 6档43v       7档，45.5v   8档 51v  9档，57v    10档61v
 * 
 * 模式2
 * 1档37v   2档41v  3档44v  4档45.5v   5档48v
 * 6档52v   7档54v  8档59v  9档64v     10档64v
 * 模式3
 * 1档42v   2档46v  3档48v  4档50v   5档52v
 * 6档55v   7档58v  8档63v  9档64v     10档64v
 * 
 * 
 *********************************************/

typedef struct 
{
    LEVEL_e level;
    int dac_data;/* data */
}DAC_DATA_t;

typedef enum{
    MIN_EMS_MAX_DAC_DAT = 2300,     // 电压最低，dac最小 
    MAX_EMS_MIN_DAC_DAT = 1020,     // 电压最高，dac 最小 --

}EMS_VOL_DAC_DAT_e;


DAC_DATA_t dac_voltage_item[] = {

    {LEVEL_0, 4090},  

    /**************
     *   3.0   4096
     *   1.7   ----x    x = 2321
     *   0.75  ----x    x = 1024   
     *      * 
    */

	// {LEVEL_1, 1900},     // 最低1.7v --- 第一次试产版本电压
    // {LEVEL_2, 1800},
    // {LEVEL_3, 1750}, 
    // {LEVEL_4, 1700},    //------------
    // {LEVEL_5, 1650},
    // {LEVEL_6, 1550},    
    // {LEVEL_7, 1500},   
    // {LEVEL_8, 1350},
    // {LEVEL_9, 1200},   
    // {LEVEL_10, 1100},	        // 对应dac 2.0v ---ems 后级输出电压30v
    {LEVEL_1, 2150},    //  20250421 根据覃峰反馈，将1档调低  （2150）vpp= 21v
    {LEVEL_2, 2050},    //   [ 2000 ]= 27v
    {LEVEL_3, 1900},    //   [ 1900 ]= 31v
    {LEVEL_4, 1800},    //--- [ 1800 ]= 35v
    {LEVEL_5, 1750},    //--- [ 1750 ]= 37v
    {LEVEL_6, 1650},     //--- [ 1650 ]= 41v
    {LEVEL_7, 1500},     //--- [ 1500 ]= 46.5v
    {LEVEL_8, 1450},     //--- [ 1400 ]= 50v
    {LEVEL_9, 1350},      //--- [ 1300 ]= 54v
    {LEVEL_10, 1250},	   //--- [ 1250 ]= 56v      // 对应dac 2.0v ---ems 后级输出电压30v

};


/**
 * @brief  Configures the different GPIO ports.
 */
static void dac_gpio_cfg(void)
{
    GPIO_InitType GPIO_InitStructure;
    GPIO_InitStruct(&GPIO_InitStructure);
    /* GPIOA Periph clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    /* Once the DAC channel is enabled, the corresponding GPIO pin is automatically
       connected to the DAC converter. In order to avoid parasitic consumption,
       the GPIO pin should be configured in analog */
    GPIO_InitStructure.Pin       = GPIO_PIN_4;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Input;
    GPIO_InitStructure.GPIO_Pull = GPIO_No_Pull;
    //GPIO_InitStructure.GPIO_Pull = GPIO_Mode_AF_OD; 
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}

/**
 * @brief  Configures the different GPIO ports.
 */
static void dac_channel_cfg(void)
{
    DAC_InitType DAC_InitStructure;

    /* DAC Periph clock enable */
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_DAC, ENABLE);
    /* DAC channel1 Configuration */
    DAC_InitStructure.Trigger          = DAC_TRG_SOFTWARE;
    DAC_InitStructure.WaveGen          = DAC_WAVEGEN_NOISE;
    DAC_InitStructure.LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
    DAC_InitStructure.BufferOutput     = DAC_BUFFOUTPUT_ENABLE;
    DAC_Init(&DAC_InitStructure);

    /* Enable DAC Channel1: Once the DAC channel1 is enabled, PA.04 is
       automatically connected to the DAC converter. */
      
    DAC_Enable(ENABLE);

    /* Set DAC Channel1 DHR12L register */
    //DAC_SetChData(DAC_ALIGN_R_12BIT, 4094);
    DAC_SetChData(DAC_ALIGN_R_12BIT, MIN_EMS_MAX_DAC_DAT);
}

void ems_voltage_select(void)
{
    int i;

    for(i = 0; i < sizeof(dac_voltage_item)/sizeof(dac_voltage_item[0]); i ++)
    {
        if(sys_data.level == dac_voltage_item[i].level){
            break;
        }
    }
    if(i == sizeof(dac_voltage_item)/sizeof(dac_voltage_item[0]))
    {
		printf("\n\r dac_index not match \n\r");
        return;
    }

    DAC_SetChData(DAC_ALIGN_R_12BIT, dac_voltage_item[i].dac_data);
    DAC_SoftTrgEnable(ENABLE);
//	printf("\n\r set_dac_data:%d \n\r", dac_voltage_item[i].dac_data);
}

void ems_voltage_percent_select(int percent)
{
    int i;
    int tmp_dac;

    for(i = 0; i < sizeof(dac_voltage_item)/sizeof(dac_voltage_item[0]); i ++)
    {
        if(sys_data.level == dac_voltage_item[i].level){
            break;
        }
    }
    if(i == sizeof(dac_voltage_item)/sizeof(dac_voltage_item[0]))
    {
		//printf("\n\r dac_index not match \n\r");
        return;
    }

    if(percent > 100){
        percent = 100;
    }

#if 0
	// max 模式，档位电压比其他模式的电压高;
    if(MAX_MODE == get_sys_mode()){

        if(OFF_LINE == get_zhan_zhi_dao_state()){
            DAC_SetChData(DAC_ALIGN_R_12BIT, 500 + (dac_voltage_item[i].dac_data)*percent/100);
        }
        else{
            // DAC_SetChData(DAC_ALIGN_R_12BIT, 250 + (dac_voltage_item[i].dac_data)*percent*3/400);
             DAC_SetChData(DAC_ALIGN_R_12BIT, 500 + (dac_voltage_item[i].dac_data)*percent/100);
        }

        
    }
    else if(BODY_MODE == get_sys_mode()){
        if(OFF_LINE == get_zhan_zhi_dao_state()){
            DAC_SetChData(DAC_ALIGN_R_12BIT, 300 + (dac_voltage_item[i].dac_data)*percent/100);
        }
        else{
            // DAC_SetChData(DAC_ALIGN_R_12BIT, 150 + (dac_voltage_item[i].dac_data)*percent*3/400);
            DAC_SetChData(DAC_ALIGN_R_12BIT, 300 + (dac_voltage_item[i].dac_data)*percent/100);
        }
    }
    else if(HAIR_MODE == get_sys_mode()){
        if(OFF_LINE == get_zhan_zhi_dao_state()){
            DAC_SetChData(DAC_ALIGN_R_12BIT, 300 + (dac_voltage_item[i].dac_data)*percent/100);
        }
        else{
            // DAC_SetChData(DAC_ALIGN_R_12BIT, 150 + (dac_voltage_item[i].dac_data)*percent*3/400);
            DAC_SetChData(DAC_ALIGN_R_12BIT, 300 + (dac_voltage_item[i].dac_data)*percent/100);
        }
        
    }
    else{
        if(OFF_LINE == get_zhan_zhi_dao_state()){
            DAC_SetChData(DAC_ALIGN_R_12BIT, (dac_voltage_item[i].dac_data)*percent/100);
        }
        else{
             DAC_SetChData(DAC_ALIGN_R_12BIT, (dac_voltage_item[i].dac_data)*percent/100);
            // DAC_SetChData(DAC_ALIGN_R_12BIT, (dac_voltage_item[i].dac_data)*percent*3/400);
        }
    }
#else
	// // max 模式，档位电压比其他模式的电压高;
    if(MAX_MODE == get_sys_mode()){
        tmp_dac =  MIN_EMS_MAX_DAC_DAT -(MIN_EMS_MAX_DAC_DAT - dac_voltage_item[i].dac_data)*percent/100 - 300;
        if(ON_LINE == get_zhan_zhi_dao_state()){
            tmp_dac = tmp_dac*12/10;            // 数值变大，电压变小
        }

    }
    else if(BODY_MODE == get_sys_mode()){
        tmp_dac =  MIN_EMS_MAX_DAC_DAT -(MIN_EMS_MAX_DAC_DAT - dac_voltage_item[i].dac_data)*percent/100 - 200;
        if(ON_LINE == get_zhan_zhi_dao_state()){
            tmp_dac = tmp_dac*12/10;            // 数值变大，电压变小
        }
    }
    else if(HAIR_MODE == get_sys_mode()){
        tmp_dac =  MIN_EMS_MAX_DAC_DAT -(MIN_EMS_MAX_DAC_DAT - dac_voltage_item[i].dac_data)*percent/100 - 300;
        if(ON_LINE == get_zhan_zhi_dao_state()){
            tmp_dac = tmp_dac*12/10;            // 数值变大，电压变小
        }
    }
    else{
        tmp_dac =  MIN_EMS_MAX_DAC_DAT -(MIN_EMS_MAX_DAC_DAT - dac_voltage_item[i].dac_data)*percent/100;
        if(ON_LINE == get_zhan_zhi_dao_state()){
            tmp_dac = tmp_dac*12/10;            // 数值变大，电压变小
        }
    }

    // tmp_dac =  MIN_EMS_MAX_DAC_DAT -(MIN_EMS_MAX_DAC_DAT - dac_voltage_item[i].dac_data)*percent/100 ;

    /********将dac输出 控制在可以调节电压的范围***************/ 
    if(tmp_dac > MIN_EMS_MAX_DAC_DAT){
        tmp_dac = MIN_EMS_MAX_DAC_DAT;
    }
    else if(tmp_dac < MAX_EMS_MIN_DAC_DAT){
        tmp_dac = MAX_EMS_MIN_DAC_DAT;
    }

    DAC_SetChData(DAC_ALIGN_R_12BIT, tmp_dac);
#endif
    DAC_SoftTrgEnable(ENABLE);
	// printf("\n\r set_dac_data:,%d \n\r", dac_voltage_item[i].dac_data*percent/100);
}



void my_dac_init(void)
{
    dac_gpio_cfg();
    dac_channel_cfg();
    DAC_SoftTrgEnable(ENABLE);
}
